package cn.sytton.taffe.job.controller;

import cn.sytton.taffe.job.entity.JobEntity;
import cn.sytton.taffe.job.service.IJobService;
import com.github.pagehelper.PageInfo;
import cn.sytton.taffe.framework.base.controller.BaseController;
import cn.sytton.taffe.framework.base.entity.PageParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 定时任务控制层
 *
 * @author skyrock
 */
@Tag(name = "定时任务")
@RestController
@RequiredArgsConstructor
@RequestMapping("/job")
public class JobController extends BaseController {

    private final IJobService jobService;

    @Operation(summary = "查询分页定时任务列表")
    @Parameter(name = "pp", description = "分页参数对象", in = ParameterIn.QUERY, required = true)
    @GetMapping
    public PageInfo<JobEntity> findPage(PageParam pp) {
        return jobService.findListPageOfXml(pp);
    }

    @Operation(summary = "查询单个定时任务")
    @Parameter(name = "id", description = "定时任务ID", in = ParameterIn.PATH, required = true)
    @GetMapping("/{id}")
    public JobEntity findById(@PathVariable String id) {
        return jobService.findById(id);
    }

    @Operation(summary = "添加定时任务")
    @PostMapping
    public void create(@RequestBody @Validated JobEntity entity) {
        jobService.create(entity);
    }

    @Operation(summary = "更新定时任务")
    @PostMapping("/update")
    public void update(@RequestBody @Validated JobEntity entity) {
        jobService.update(entity);
    }

    @Operation(summary = "删除定时任务")
    @PostMapping("/delete")
    public void delete(@RequestBody String[] ids) {
        jobService.delete(ids);
    }

    @Operation(summary = "修改定时任务状态")
    @PostMapping("/status/update")
    public void updateStatus(@RequestBody JobEntity entity) {
        jobService.updateStatus(entity);
    }

    @Operation(summary = "定时任务立即执行一次")
    @PostMapping("/run")
    public void run(@RequestBody JobEntity entity) {
        jobService.run(entity);
    }

    @Operation(summary = "查询任务运行统计信息")
    @Parameter(name = "id", description = "定时任务ID", in = ParameterIn.PATH, required = true)
    @GetMapping("/run/info/{id}")
    public Map<String, Object> findRunInfo(@PathVariable String id) {
        return jobService.findRunInfo(id);
    }
}
